package yf.mall.controller;

import java.util.List;

import com.yf.response.RRException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;

import java.util.Map;

import com.yf.consts.DaoConst;
import yf.mall.entity.StoresEntity;
import yf.mall.entity.UserEntity;
import yf.mall.service.StoresService;
import com.yf.response.R;
import yf.mall.service.UserService;



@RestController
@RequestMapping("/stores")
public class StoresController {
    @Autowired
    private StoresService storesService;

    /**
     * 列表线下店铺
     */
    @SysLog("查看线下店铺")
    @RequestMapping("/list")
    @RequiresPermissions("stores:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageHelper.startPage(DaoConst.getPage(params), DaoConst.getLIMIT(params));
        String userCode = (String) params.get("userCode");
        String refCode = (String) params.get("refCode");
        if (refCode != null && !"".equals(refCode)){
            UserEntity refCodeEntity = userService.findByCode(refCode);
            if (refCodeEntity == null){
                throw new RRException("管理员编号不存在");
            }
            params.put("refId",refCodeEntity.getId());
        }
        if (userCode != null && !"".equals(userCode)){
            UserEntity uCodeEntity = userService.findByCode(userCode);
            if (uCodeEntity == null){
                throw new RRException("用户编号不存在");
            }
            params.put("userId",uCodeEntity.getId());
        }

        List<StoresEntity> storesList = storesService.queryList(params);
        if (storesList!=null&&storesList.size()>0){
            storesList.forEach(storesEntity -> {
                if (storesEntity.getRefId()!=null) {
                    UserEntity refUser = userService.get(storesEntity.getRefId());
                    if (refUser!=null){
                        storesEntity.setRefCode(refUser.getCode());
                    }
                }
                if (storesEntity.getUserId()!=null) {
                    UserEntity user = userService.get(storesEntity.getUserId());
                    if (user != null){
                        storesEntity.setUserCode(user.getCode());
                    }
                }
            });
        }
        return R.ok(new PageInfo(storesList));
    }

    /**
     * 查看线下店铺
     */
    @SysLog("查看线下店铺")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("stores:info")
    public R info(@PathVariable("id") Long id) {
        StoresEntity stores = storesService.get(id);
        if (stores.getUserId()!=null) {
            stores.setUserCode(userService.get(stores.getUserId()).getCode());
        }
        if (stores.getRefId()!=null) {
            stores.setRefCode(userService.get(stores.getRefId()).getCode());
        }
        return R.ok(stores);
    }


    @Autowired
    private UserService userService;

    /**
     * 保存线下店铺
     */
    @SysLog("保存线下店铺")
    @RequestMapping("/save")
    @RequiresPermissions("stores:save")
    public R save(StoresEntity stores) {
        stores = codeIsExist(stores);
        StoresEntity storesEntity = storesService.findByUserCode(stores.getUserCode());
        if (storesEntity != null) {
            throw new RRException("该用户已有线下店铺");
        }
        if (storesService.findByName(stores.getName())!=null) {
            throw new RRException("该店铺名已存在");
        }
        storesService.save(stores);
        return R.ok(stores);
    }


    /**
     * 修改线下店铺
     */
    @SysLog("修改线下店铺")
    @RequestMapping("/update")
    @RequiresPermissions("stores:update")
    public R update(StoresEntity stores) {
        stores = codeIsExist(stores);
        storesService.update(stores);

        return R.ok(stores);
    }

    /**
     * 删除线下店铺
     */
    @SysLog("删除线下店铺")
    @RequestMapping("/delete")
    @RequiresPermissions("stores:delete")
    public R delete(Long[] ids) {
        storesService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有线下店铺
     */
    @SysLog("所有线下店铺")
    @RequestMapping("/all")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<StoresEntity> storesList = storesService.queryList(params);

        return R.ok(storesList);
    }

    /***********自定义开始***********/
    /**
     * Long
     * 判断用户跟推荐人用户是否存在
     *
     * @param stores
     * @return
     */
    private StoresEntity codeIsExist(StoresEntity stores) {
        if (stores.getRefCode() != null) {
            UserEntity refUser = userService.findByCode(stores.getRefCode());
            if (refUser == null) {
                throw new RRException("推荐人编号不存在");
            }
            stores.setRefId(refUser.getId());
        }
        UserEntity userEntity = userService.findByCode(stores.getUserCode());

        if (userEntity == null) {
            throw new RRException("用户编号不存在");
        }
        stores.setUserId(userEntity.getId());
        return stores;
    }

}
